<?php
class Template
{
    const DB_FILE = 'db/templates.db';

    private static $instance = false;
    private $errors = array();
    private $pdo = null;

    private function __construct()
    {
        $dsn = 'sqlite:' . realpath(dirname(__FILE__) . '/../' . self::DB_FILE);

        try
        {
            $this->pdo = new PDO($dsn);
        }
        catch(PDOException $e)
        {
            die($e->getMessage());
        }
    }

    private function __clone()
    {
    }

    private static function getInstance()
    {
        if (self::$instance === false)
        {
            self::$instance = new Template();
        }

        return self::$instance;
    }

    public static function getErrors()
    {
        return self::getInstance()->errors;
    }

    public static function getList($locale, $sortField = false, $sortOrder = false)
    {
        $query = 'SELECT [locale], [name], [description], [subject] FROM [templates] WHERE [locale]=?';

        if (!empty($sortField))
        {
            $query .= ' ORDER BY [' . addslashes($sortField) . '] ' . ((strtoupper($sortOrder) == 'ASC') ? 'ASC' : 'DESC');
        }

        $statement = self::getInstance()->pdo->prepare($query);
        $statement->execute(array($locale));

        return $statement->fetchAll(PDO::FETCH_ASSOC);
    }

    public static function getOne($locale, $name)
    {
        $query = 'SELECT [locale], [name], [description], [subject], [body] FROM [templates] WHERE [locale]=? AND [name]=?';

        $statement = self::getInstance()->pdo->prepare($query);
        $statement->execute(array($locale, $name));

        return $statement->fetch(PDO::FETCH_ASSOC);
    }

    public static function save($locale, $name, $description, $subject, $body)
    {
        $query = 'SELECT COUNT(*) FROM [templates] WHERE [locale]=? AND [name]=?';

        $statement = self::getInstance()->pdo->prepare($query);
        $statement->execute(array($locale, $name));

        $result = $statement->fetch(PDO::FETCH_NUM);

        if (empty($result[0])) // new record
        {
            $query = 'INSERT INTO [templates] ([description], [subject], [body], [locale], [name]) VALUES (?, ?, ?, ?, ?)';
        }
        else // update record
        {
            $query = 'UPDATE [templates] SET [description]=?, [subject]=?, [body]=? WHERE [locale]=? AND [name]=?';
        }

        $statement = self::getInstance()->pdo->prepare($query);
        $statement->execute(array($description, $subject, $body, $locale, $name));

        return (empty($result[0])) ? self::getInstance()->pdo->lastInsertId() : $statement->rowCount();
    }

    public static function delete($locale, $name)
    {
        $query = 'DELETE FROM [templates] WHERE [locale]=? AND [name]=?';

        $statement = self::getInstance()->pdo->prepare($query);
        $statement->execute(array($locale, $name));

        return $statement->rowCount();
    }
}
